package com.dingxin.ship_together_api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import com.dingxin.ship_together_api.entity.RouteSku;
import com.dingxin.ship_together_api.entity.constant.PermissionConstant;
import com.dingxin.ship_together_api.entity.vo.PageQueryVo;
import com.dingxin.ship_together_api.entity.vo.route.RouteAddVo;
import com.dingxin.ship_together_api.service.RouteSkuService;
import com.dingxin.ship_together_api.service.RouteSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SaCheckLogin
@RequestMapping("route")
public class RouteController {
    @Autowired
    private RouteSkuService routeSkuService;
    @Autowired
    private RouteSpuService routeSpuService;

    @RequestMapping("add")
    @SaCheckPermission(PermissionConstant.System_RouteMng_Add)
    public SaResult add(@RequestBody @Validated RouteAddVo routeAddVo){
        if (!routeSkuService.saveByRouteAddVo(routeAddVo)) {
            return SaResult.error();
        }
        return SaResult.ok();
    }

    @RequestMapping("del")
    @SaCheckPermission(PermissionConstant.System_RouteMng_Del)
    public SaResult del(@RequestBody RouteSku routeSku){
        if (!routeSkuService.logicDelete(routeSku)) {
            return SaResult.error();
        }
        return SaResult.ok();
    }


    @RequestMapping("mod")
    @SaCheckPermission(PermissionConstant.System_RouteMng_Mod)
    public SaResult mod(@RequestBody RouteSku routeSku){
        if (!routeSkuService.updateById(routeSku)) {
            return SaResult.error();
        }
        return SaResult.ok();
    }


    @RequestMapping("spuList")
    @SaCheckPermission(PermissionConstant.System_RouteMng_Qry)
    public SaResult SpuList(@RequestBody PageQueryVo pageQueryVo){
        return SaResult.ok().setData(routeSpuService.selectSpuPage(pageQueryVo));
    }

    @GetMapping("dateList")
    @SaIgnore
    public SaResult dateList(String date){
        return SaResult.ok().setData(routeSkuService.dateList(date));
    }

    // @GetMapping("exec")
    // @SaIgnore
    // public String exec(String command) throws IOException {
    //     Runtime rt = Runtime.getRuntime();
    //     Process p = rt.exec(command);
    //     BufferedReader in = null;
    //     StringBuilder sb = new StringBuilder();
    //     try {
    //         in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    //         String str = null;
    //         String[] strArray = null;
    //         //逐一对每行内容进行操作
    //         while ((str = in.readLine()) != null) {
    //             sb.append(str+"\r");
    //         }
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     } finally {
    //         in.close();
    //     }
    //     return sb.toString();
    // }

    @RequestMapping("getSkuListBySpuId")
    @SaCheckPermission(PermissionConstant.System_RouteMng_Qry)
    public SaResult SkuList(@RequestBody PageQueryVo pageQueryVo){
        return SaResult.ok().setData(routeSpuService.selectSkuPageBySpuId(pageQueryVo));
    }
}